﻿@model GDoc.Web.Models.AccountUsersModel
@{
    ViewBag.Title = "Usuarios y Grupos";
}
@section Head
{
    <script type="text/javascript">
        $(document).ready(function () {
            $("#add-new-group").click(function () {
                $("#dialog:ui-dialog").dialog("destroy");
                $("#name").val('');
                $("#add-new-group-dialog-form").dialog({
                    autoOpen: true,
                    height: "auto",
                    width: "auto",
                    modal: true,
                    resizable: false,
                    buttons: {
                        "Crear": function () {
                            var bValid = true;

                            bValid = bValid && checkLength($("#name"), "name", 1, 20);

                            if (bValid) {

                                $.ajax({
                                    url: '@Url.Action("AddGroup", "Account")',
                                    data: { name: $("#name").val() },
                                    type: "POST",
                                    cache: false,
                                    success: function (data) {
                                        $("#all-user-groups").append('<li class="account-group" id="' + data.Id + '"><a href="' + data.Ref + '">' + data.Name + '</a></li>');
                                    },
                                    error: function (data) {
                                        var message = 'No se ha podido crear el grupo ' + $("#name").val() + '.'

                                        if (data.status == 409)
                                            message += '<br /><br />Ya existe el grupo ' + $("#name").val() + '.';

                                        showErrorDialog('Error', message);
                                    }
                                });

                                $(this).dialog("destroy");
                            }
                        },
                        Cancel: function () {
                            $(this).dialog("destroy");
                        }
                    },
                    close: function () {
                        $(this).dialog("destroy");
                    }
                });
            });
        });

        function deleteGroup(id, name){
            showDeleteDialog(
                'Eliminar Grupo',
                '¿Está seguro que desea eliminar el grupo ' + name + '?',
                function () {
                    $.ajax({
                        url: '@Url.Action("RemoveGroup", "Account")',
                        data: { id: id },
                        type: "POST",
                        cache: false,
                        success: function (data) {
                            $("#all-user-groups").find("li#" + id).remove();
                        },
                        error: function (data) {
                            showErrorDialog(
                                'Error',
                                'No se ha podido eliminar el grupo ' + name + '.');
                        }
                    });
                });
        }
    </script>
}
<div class="navigation-panel">
    @Html.ActionLink(Model.Account.Name, "Index", "Account") > Usuarios y Grupos
</div>
<div class="users-panel list-panel" style="width: 65%; display: inline-block; position: relative; float: left;">
    <div><a id="add-new-user" class="list-panel-h3-add-link" href="#">+Nuevo</a></div>
    <h3>Usuarios</h3>
    <ul id="all-users-list">
        @foreach (var u in Model.Account.Users)
        { 
            <li class="account-user" id="@u.Id">@Html.RouteLink(u.User.Email, "AccountUser", new { id = u.Id}) <span>@(u.Role.Enum != GDoc.Domain.RoleEnum.Member ? String.Format("({0})", u.Role.Enum.ToString().ToLower()) : "")</span> <span class="hidden-action"> ~ <a href="#">eliminar</a></span></li>
        }
    </ul>
</div>
<div class="groups-panel list-panel" style="width: 30%; display: inline-block; position: relative; float: right;">
    <div><a id="add-new-group"class="list-panel-h3-add-link" href="#">+Nuevo</a></div>
    <h3>Grupos</h3>
    <ul id="all-user-groups">
        @foreach (var g in Model.Account.Groups)
        {
            <li class="account-group" id="@g.Id">@Html.RouteLink(g.Name, "AccountGroup", new { id = g.Id }) <span class="hidden-action"> ~ <a href="javascript:deleteGroup(@g.Id, '@Html.Encode(g.Name)')">eliminar</a></span></li>
        }
    </ul>
</div>
<div id="add-new-group-dialog-form" title="Nuevo Grupo" style="display: none">
    <form>
    <label for="name">
        Nombre</label>
    <input type="text" name="name" id="name" class="text ui-widget-content ui-corner-all" />
    </form>
</div>